import { Tween, tween } from "cc";
import UICore from "../../../../Core/Res/UICore";
import { RandomUtils } from "../../../../Core/Utils/RandomUtils";
import ConfigManager from "../../../Config/ConfigManager";
import AbstractUIWindow from "../../window/AbstractUIWindow";
import * as fgui from "fairygui-cc";
import { ViewManager } from "../../base/ViewManager";
import SdkTool from "../../../../Core/Sdk/SdkTool";
import { GameModels } from "../../socket/GameModels";
import EventMgr from "../../../../Core/Event/EventMgr";
import { support } from "../../../proto/protoCmd";
import { PrizeDraw } from "../../../Config/schema/schema";

export class TurntableWindow extends AbstractUIWindow {
    private pointer: fgui.GImage;
    private start: fgui.GButton;
    protected getResList(): Array<string> {
        return ["Main"];
    }
    protected onInit(): void {
        this._view = UICore.createObject("Main", "TurntableWindow").asCom;
    }
    protected onShow(): void {
        EventMgr.on(`event_RespTakeLuckyDrawAward`, this.event_RespTakeLuckyDrawAward, this);
        this.start.onClick(this.clickstart, this);
    }
    private event_RespTakeLuckyDrawAward(info: support.RespTakeLuckyDrawAward): void {
        this.startPrizeDraw(ConfigManager.tables.TbPrizeDraw.get(info.AwardId));
    }
    private startPrizeDraw(cfg: PrizeDraw): void {
        this.pointer.rotation = 0;
        tween(this.pointer).to(3, { rotation: (3 * 360) + cfg.angle + 22 }, { easing: `circOut` }).call(() => {
            ViewManager.ins.showtips(`恭喜你获得${cfg.name}!`);
        }, this).start();
    }
    private clickstart(): void {
        SdkTool.ins.sdk.playRewardAd((play) => {
            if (play) {
                // this.startPrizeDraw();
                GameModels.support.send_ReqTakeLuckyDrawAward(1);
            }
        }, this);
    }
    protected onClose(): void {
        Tween.stopAllByTarget(this.pointer);
    }
}